
public class ProductView extends View implements RectangularButtonDelegate{

	private Product displayed;
	private QuantitiesView quantityView;
	private RectangularButton newQuantity, acceptChanges, cancelChanges;
	private TextBox productName, productCategory, productHeight, productWidth, productDepth,productMonthlySales, productPriority, productPrice, productDiscount;
	private boolean newProductMode, editProductMode;
	private String message;
	public ProductView(float x_, float y_, float w_, float h_, MainView p) {
		super(x_, y_, w_, h_, p);
		// TODO Auto-generated constructor stub
		this.quantityView = new QuantitiesView(20, 200, 780, 300,parent);
		
		this.newQuantity = new RectangularButton(w/2 - 100, h - 50, 200, 30, parent,4);
		this.newQuantity.setCaption("Add Quantity");
		this.newQuantity.setDelegate(this);
		
		this.acceptChanges = new RectangularButton(w/2 - 250, h - 50, 200, 30, parent,5);
		this.acceptChanges.setCaption("Save Changes");
		this.acceptChanges.setDelegate(this);
		
		this.cancelChanges = new RectangularButton(w/2, h - 50, 200, 30, parent,6);
		this.cancelChanges.setCaption("Cancel Changes");
		this.cancelChanges.setDelegate(this);
		this.message= "";
		
		
		// public TextBox(float x_, float y_, float w_, float h_,MainView p)
		this.productName = new TextBox(200, 5, 500, 20, parent, "Enter Product Name");
		
		this.productCategory = new TextBox(200, 45, 500, 20, parent, "Enter Product Category");
		
		this.productHeight = new TextBox(200, 85, 500, 20, parent, "Enter Product Height");
		
		this.productWidth = new TextBox(200, 125, 500, 20, parent, "Enter Product Width");
		
		this.productDepth = new TextBox(200, 165, 500, 20, parent, "Enter Product Depth");
		
		this.productMonthlySales = new TextBox(200, 205, 500, 20, parent, "Enter Product MonthlySales");
		
		this.productPriority = new TextBox(200, 245, 500, 20, parent, "Enter Product Priority");
		
		this.productPrice = new TextBox(200, 285, 500, 20, parent, "Enter Product Price");
		
		this.productDiscount = new TextBox(200, 325, 500, 20, parent, "Enter Product Discount");
		
		this.newProductMode= false;
		
		this.editProductMode = false;
	}
	void drawContent()
	  {
		parent.fill(255);
		parent.rect(0,0,w,h);
		parent.fill(0);
		
		if(displayed != null && !this.newProductMode && !this.editProductMode){

			parent.textAlign(parent.LEFT,parent.BASELINE);
			parent.textSize(18);
			parent.text("Product Name: ", 20, 20);
			parent.text("Category: ", 20, 40);
			parent.text("Height: ", 20, 60);
			parent.text("Width: ", 20, 80);
			parent.text("Depth: ", 20, 100);
			parent.text("Monthly Sales: ", 20, 120);
			parent.text("Priority: ", 20, 140);
			parent.text("Price: ", 20, 160);
			parent.text("Discount: ", 20, 180);
			parent.fill(24,116,205);
			parent.text(displayed.getProductName(), 200, 20);
			parent.text(displayed.getCategory(), 200, 40);
			parent.text(displayed.getHeight(), 200, 60);
			parent.text(displayed.getWidth(), 200, 80);
			parent.text(displayed.getDepth(), 200, 100);
			parent.text(displayed.getMonthlySales(), 200, 120);
			parent.text(displayed.getPriority(), 200, 140);
			parent.text("N/A", 200, 160);
			parent.text("N/A", 200, 180);
			//System.out.println("Currently disaplying "+ this.displayed.getProductName());
			//parent.text(displayed.getProductName(),100,100);
		}
		if(this.newProductMode){
			parent.textAlign(parent.LEFT,parent.BASELINE);
			parent.textSize(18);
			parent.text("Product Name: ", 20, 20);
			parent.text("Category: ", 20, 60);
			parent.text("Height: ", 20, 100);
			parent.text("Width: ", 20, 140);
			parent.text("Depth: ", 20, 180);
			parent.text("Monthly Sales: ", 20, 220);
			parent.text("Priority: ", 20, 260);
			parent.text("Price: ", 20, 300);
			parent.text("Discount: ", 20, 340);
		}
		
		if(this.editProductMode){
			
			parent.textAlign(parent.LEFT,parent.BASELINE);
			parent.textSize(18);
			parent.fill(24,116,205);
			parent.text(displayed.getProductName(), 200, 20);
			parent.text("Product Name: ", 20, 20);
			parent.text("Category: ", 20, 60);
			parent.text("Height: ", 20, 100);
			parent.text("Width: ", 20, 140);
			parent.text("Depth: ", 20, 180);
			parent.text("Monthly Sales: ", 20, 220);
			parent.text("Priority: ", 20, 260);
			parent.text("Price: ", 20, 300);
			parent.text("Discount: ", 20, 340);
		}
		
		
		
	  }

	boolean contentReleased(float lx, float ly){
		if(!editProductMode){
		displayed = parent.getDragged();
		parent.setDragged(null);
		parent.setDragging(false);
		if(displayed != null){
		this.quantityView.setQuantities(displayed.getQuantities());
		if(!this.subviews.contains(this.quantityView))this.subviews.add(this.quantityView);
		if(!this.subviews.contains(this.newQuantity))this.subviews.add(this.newQuantity);
		}}
		
		
		return true;
	}
	
	 boolean mousePressed(float px, float py)
	    {
			productName.setActivated(false);
			productCategory.setActivated(false);
			productWidth.setActivated(false);
			productHeight.setActivated(false);
			productDepth.setActivated(false);
			productMonthlySales.setActivated(false);
			productPriority.setActivated(false);
			productPrice.setActivated(false);
			productDiscount.setActivated(false);

	        return super.mousePressed(px, py);
	    }
	
	public void RectangularButtonPressed( int id){
		if(id == 1 && !newProductMode && !editProductMode){
			//Add a new Product
			parent.removeSearchView();
			if(this.subviews.contains(this.newQuantity))this.subviews.remove(this.newQuantity);
			if(this.subviews.contains(this.quantityView))this.subviews.remove(this.quantityView);
			this.subviews.add(this.productCategory);
			this.subviews.add(this.productDepth);
			this.subviews.add(this.productDiscount);
			this.subviews.add(this.productHeight);
			this.subviews.add(this.productName);
			this.subviews.add(this.productPrice);
			this.subviews.add(this.productPriority);
			this.subviews.add(this.productWidth);
			this.newProductMode = true;
			this.subviews.add(this.productMonthlySales);
			this.subviews.add(this.acceptChanges);
			this.subviews.add(this.cancelChanges);
			return;
		}
		if(id == 2 && displayed != null && !newProductMode && !editProductMode){
			System.out.println(this.displayed.toString());
			parent.removeSearchView();
			if(this.subviews.contains(this.newQuantity))this.subviews.remove(this.newQuantity);
			if(this.subviews.contains(this.quantityView))this.subviews.remove(this.quantityView);
			this.subviews.add(this.productCategory);
			this.subviews.add(this.productDepth);
			this.subviews.add(this.productDiscount);
			this.subviews.add(this.productHeight);
			
			this.subviews.add(this.productPrice);
			this.subviews.add(this.productPriority);
			this.subviews.add(this.productWidth);
			this.editProductMode = true;
			this.subviews.add(this.productMonthlySales);
			this.subviews.add(this.acceptChanges);
			this.subviews.add(this.cancelChanges);
		}
		if(id == 4){
			System.out.println("Add a new quantity:");
			// int count, int year, int day, int month
			Quantity qtemp = new Quantity(10,2012,10,10);
			this.quantityView.addQuantity(qtemp);
			this.quantityView.updateSubViews();
			// Window for entering quantity
			return;
		}
		if(id == 5 && !newProductMode && !editProductMode){
			if(this.newProductMode){
			try{
				//public Product(int category, String name, int height, int depth, int width, int priority, int sales, int count)
				
				// Name has to be included
				if(!this.productName.getText().equals("")){
					int category = 0, height = 0, depth = 0, width = 0, priority = 0, sales = 0, count = 0, discount = 0, price  = 0;
					if(!this.productCategory.getText().equals(""))
						category = Integer.parseInt(this.productCategory.getText());
					if(!this.productHeight.getText().equals(""))
						height = Integer.parseInt(this.productHeight.getText());
					if(!this.productDepth.getText().equals(""))
						depth = Integer.parseInt(this.productDepth.getText());
					if(!this.productWidth.getText().equals(""))
						width = Integer.parseInt(this.productWidth.getText());
					if(!this.productPriority.getText().equals(""))
						priority = Integer.parseInt(this.productPriority.getText());
					if(!this.productMonthlySales.getText().equals(""))
						sales = Integer.parseInt(this.productMonthlySales.getText());
					if(!this.productDiscount.getText().equals(""))
						discount = Integer.parseInt(this.productDiscount.getText());
					if(!this.productPrice.getText().equals(""))
						price = Integer.parseInt(this.productPrice.getText());
					
			Product p = new Product( category, this.productName.getText(), height,depth,width,priority,sales, 0);
			parent.productsList.add(p);
			System.out.println("Product Information:\n"+p.toString()+" Discount = "+discount+" Price = "+price);
				}
			parent.addSearchView();
			
		
			this.subviews.remove(this.productCategory);
			this.subviews.remove(this.productDepth);
			this.subviews.remove(this.productDiscount);
			this.subviews.remove(this.productHeight);
			this.subviews.remove(this.productName);
			this.subviews.remove(this.productPrice);
			this.subviews.remove(this.productPriority);
			this.subviews.remove(this.productWidth);
			this.newProductMode = false;
			this.subviews.remove(this.productMonthlySales);
			this.subviews.remove(this.acceptChanges);
			this.subviews.remove(this.cancelChanges);
			
			}
			catch(NumberFormatException e){
				parent.setMainScreenMessage("Product couldn't be saved. Wrong input format.", 300, 50);
			}
			}

			if(this.editProductMode){
				try{
					//public Product(int category, String name, int height, int depth, int width, int priority, int sales, int count)
					
					// Name has to be included
					int discount = 0, price = 0;
						if(!this.productCategory.getText().equals(""))
							this.displayed.setCategory(Integer.parseInt(this.productCategory.getText()));
						if(!this.productHeight.getText().equals(""))
							this.displayed.setHeight(Integer.parseInt(this.productHeight.getText()));
						if(!this.productDepth.getText().equals(""))
							this.displayed.setDepth(Integer.parseInt(this.productDepth.getText()));
						if(!this.productWidth.getText().equals(""))
							this.displayed.setWidth(Integer.parseInt(this.productWidth.getText()));
						if(!this.productPriority.getText().equals(""))
							this.displayed.setPriority(Integer.parseInt(this.productPriority.getText()));
						if(!this.productMonthlySales.getText().equals(""))
							this.displayed.setMonthlySales(Integer.parseInt(this.productMonthlySales.getText()));
						if(!this.productDiscount.getText().equals(""))
							discount = Integer.parseInt(this.productDiscount.getText());
						if(!this.productPrice.getText().equals(""))
							price = Integer.parseInt(this.productPrice.getText());
						
				/*
				 * Replace the old product with the new product
					*/
				System.out.println("Product Information:\n"+this.displayed.toString()+" Discount = "+discount+" Price = "+price);
					
				parent.addSearchView();
				
			
				this.subviews.remove(this.productCategory);
				this.subviews.remove(this.productDepth);
				this.subviews.remove(this.productDiscount);
				this.subviews.remove(this.productHeight);
				this.subviews.remove(this.productName);
				this.subviews.remove(this.productPrice);
				this.subviews.remove(this.productPriority);
				this.subviews.remove(this.productWidth);
				this.editProductMode = false;
				this.subviews.remove(this.productMonthlySales);
				this.subviews.remove(this.acceptChanges);
				this.subviews.remove(this.cancelChanges);
				
				}
				catch(NumberFormatException e){
					parent.setMainScreenMessage("Product couldn't be saved. Wrong input format.", 300, 50);
				}
			} 
			}
		if(id == 6){
			parent.addSearchView();
			
			
			this.subviews.remove(this.productCategory);
			this.subviews.remove(this.productDepth);
			this.subviews.remove(this.productDiscount);
			this.subviews.remove(this.productHeight);
			this.subviews.remove(this.productName);
			this.subviews.remove(this.productPrice);
			this.subviews.remove(this.productPriority);
			this.subviews.remove(this.productWidth);
			this.editProductMode = false;
			this.newProductMode = false;
			this.subviews.remove(this.productMonthlySales);
			this.subviews.remove(this.acceptChanges);
			this.subviews.remove(this.cancelChanges);
		}
		
	}
}
